package excel.countryMedicine;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import po.MyObject;
import util.AbstractExl;
import util.Conts;

/**
 * @author suwy
 * @date 2014-3-11
 * @todo TODO
 */
public class BaseMedFactory  extends AbstractExl {

	private static final Logger LOG = Logger.getLogger(BaseMedFactory.class);
	private static String fileName = "基本药物生产企业入网目录";
	private static int rowNum = Conts.PAGE_BASE_MED_FACTORY;
	private static String filePath = Conts.HTM_ROOT_PATH +fileName+"/";
	private static String outputFolder = Conts.EXL_ROOT_PATH;
	private static String regex = "<tr>.*?"
		+"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">省市</td>.*?"
		+"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"
		+"<tr>.*?"
		+"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">组织机构代码</td>.*?"
		+"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"
		+"<tr>.*?"
		+"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">企业名称</td>.*?"
		+"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"
		+"<tr>.*?id is 【(.*?)】";

	private void writeExl(List<MyObject> list) {
		File folder = new File(outputFolder);
		if (!folder.exists()) folder.mkdirs();
		FileOutputStream fOut = null;
		try {
			Workbook workbook = new SXSSFWorkbook(2000);
			Sheet sheet = workbook.createSheet("Sheet1");
			Row row = null;
			fOut = new FileOutputStream(outputFolder+fileName+".xlsx");
				for (int i = 0; i < list.size(); i++) {
					row = sheet.createRow(i);
					row.createCell((short)0).setCellValue(list.get(i).getObj1().trim());
					row.createCell((short)1).setCellValue(list.get(i).getObj2().trim());
					row.createCell((short)2).setCellValue(list.get(i).getObj3().trim());
					row.createCell((short)3).setCellValue(list.get(i).getObj4().trim());
//					LOG.info("It had "+i+" rows");
				}
			workbook.write(fOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != fOut) {
				try {
					fOut.close();
					LOG.info(fileName +" success export excel ! And the excel had " + list.size() + " rows.");
				} catch (Exception e) {
					e.printStackTrace(System.err);
				}
			}
		}
	}
	
	private void excute() {
		try {
			List<MyObject> list = new ArrayList<MyObject>();;
			StringBuffer content = null;
			MyObject o = new MyObject();
			o.setObj1("REG_CODE");
			o.setObj2("ORIGINAL_REG_CODE");
			o.setObj3("REG_CODE_DESC");
			o.setObj4("RE_PACKAGE_APPROVE_CODE");
			list.add(o);
			for (int i = 1; i <= rowNum; i++) {
				content = readHtm(filePath+i+".htm");
				Pattern pattern = Pattern.compile(regex);
				Matcher matcher = pattern.matcher(content);
				while (matcher.find()) {
					o = new MyObject();
					o.setObj1(matcher.group(1).trim());
					o.setObj2(matcher.group(2).trim());
					o.setObj3(matcher.group(3).trim());
					o.setObj4(matcher.group(4).trim());
					list.add(o);
				}
				LOG.info(i + " 文件");
			}
			writeExl(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		BaseMedFactory instance = new BaseMedFactory();
		instance.excute();
	}

}
